<?php
namespace app\index\controller;
use think\Db;
use think\Controller;

class IndexController extends Controller {

	function index(){
		$ads_list = db('sys_ads')->order('add_time desc')->where('type',1)->select();
		$this->assign('ads_list',$ads_list);
		
		$agent_list = db('good_cate')
						->field('id,name,logo')
						->limit(4)
						->select();
		$this->assign('agent_list',$agent_list);
		
		$goods_list = db('goods')
						->field('goods_id,goods_name,logo,cate_id,cate2_id,cost_price,sale_price')
						->limit(5)
						->select();
		$this->assign('goods_list',$goods_list);
		return $this->fetch();
	}
	
	function agent_list(){
		$latitude = input('latitude');
		$longitude = input('longitude');
		//附近商家
		$juli = 'ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$latitude.'*PI()/180-latitude*PI()/180)/2),2)+COS('.$latitude.'*PI()/180)*COS(latitude*PI()/180)*POW(SIN(('.$longitude.'*PI()/180-longitude*PI()/180)/2),2)))*1000)';
		
		$agent_list = db('agent')
						->field('id,agent_name,address,mobile,longitude,latitude,province_name,city_name,town_name,country_name,logo,'.$juli."as juli ")
						->order("juli desc")
						->limit(3)
						->select();
		if($agent_list){
			return json(['code'=>200,'data'=>$agent_list]);
		}else{
			return json(['code'=>403,'msg'=>'暂无数据']);
		}
		
	}

	
	public function login() {
		if(session('user_id')){
			$this->redirect('index/member/index');
		}
		if (request()->isPost()) {
			$user_info = db('member')
							->where('mobile',input('mobile'))
							->where('password',md5_salt(input('password')))
							->find();
			if($user_info){
				if($user_info['status'] == 2){
					return json(['code'=>403,'msg'=>'账号已冻结']);
				}else{
					session('user_id',$user_info['user_id']);	
					return json(['code'=>200,'msg'=>'登录成功']);
				}
			}else{
				return json(['code'=>402,'msg'=>'登录失败']);	
			}
		}
		return $this->fetch();
	}
	
	/**
     * 获取验证码
     * 注意防盗刷
     * 
     */
    function verify_code(){
    	if(request()->isPost()){
    		$user_ip = $_SERVER["REMOTE_ADDR"];
    		
    		$code_info  =db('verify_code')->where('user_ip',$user_ip)->whereTime('add_time', 'today')->select();
    		
    		if(count($code_info)>10){
    			return json(['code'=>402,'msg'=>'ip异常']);
    		}else{
    			
    			return get_code($_POST['mobile'],$user_ip);
    		}
    	}
    }

	/**
	 * 注册:第一步
	 */
	function regist() {
		
		if(request()->isPost()){
			
			$result = check_sendcode($_POST['mobile'], $_POST['yzm'], '发送成功');
			if($result != 1) {
				return json(['code'=>403,'msg'=>'验证码错误或过期']);
			}
			
			//检查是否注册
			$user_info = db('member')->where('mobile',$_POST['mobile'])->find();
			if($user_info){
				return json(['code'=>402,'msg'=>'该号码已注册']);
			}else{
				if($_POST['fromuid'] && $_POST['fromuid']>0){
					
					$user_info = db('member')->where('user_id',$_POST['fromuid'])->find();
					
					if(!$user_info){
						$user_info = db('member')->where('user_id',1)->find();
					}
				}else{
					$user_info = db('member')->where('user_id',1)->find();
					
				}
				
				$data['pid1'] 			= $user_info['user_id'];
				$data['pid2'] 			= $user_info['pid1'];
				$data['province_id'] 	= $user_info['province_id'];
				$data['city_id'] 		= $user_info['city_id'];
				$data['district_id'] 	= $user_info['district_id'];
				
				$data['mobile'] 	= $_POST['mobile'];
				$data['password'] 	= md5_salt($_POST['password']);
				$data['mobile'] 	= $_POST['mobile'];
				$data['add_time'] 	= now_time();
				
				$rest=db('member')->insert($data);
				if($rest){
					return json(['code'=>200,'msg'=>'注册成功','url'=>'/index/member/index']);
				}else{
					return json(['code'=>405,'msg'=>'注册失败']);
				}
			}
		}
		
		return $this->fetch();
	}


	function forget(){
		if(request()->isPost()){
			$result = check_sendcode($_POST['mobile'], $_POST['yzm'], '发送成功');
			if($result != 1) {
				return json(['code'=>402,'msg'=>'验证码错误或过期']);
			}
			//检查是否注册
			$user_info = db('member')->where('mobile',$_POST['mobile'])->find();
			if($user_info){
				$rest = db('member')->where('user_id', $user_info['user_id'])->update(['password' => md5_salt($_POST['password'])]);
				if($rest){
					return json(['code'=>200,'msg'=>'修改成功','url'=>'/index/index/login']);
				}else{
					return json(['code'=>405,'msg'=>'修改失败']);
				}
			}else{
				return json(['code'=>403,'msg'=>'该用户不存在']);
			}
			
		}
		
		return $this->fetch();
	}
	
	public function logout() {
		session('user_id', null);
		return json(['code'=>200,'msg'=>'退出成功']);
	}
	public function xy() {
		return $this->fetch();
	}
}
